<?php
class Config {

	private static	$instance	= null;
	private static $hash 		= null;

	public static function getInstance() {
		if (self::$instance == null) {
			self::$instance = new Config();
		}
		return self::$instance;
	}

	public function __construct($config = array()) {

		if (self::$hash !== null) {
			return;
		}

		self::$hash = array();

		libxml_use_internal_errors(true);
		error_reporting(E_ALL | E_STRICT);
		set_magic_quotes_runtime(0);
		date_default_timezone_set("Africa/Casablanca"); # same as GMT
		set_error_handler(array("FrameworkException", "errorHandler"), E_ALL);

		/**
		 * default configuration values
		 */
		self::$hash["mainClass"]		= "Main";
		self::$hash["mainMethod"]		= "index";
		self::$hash["phpExt"]			= ".php";

		self::$hash["memcacheServer"]	= "localhost";
		self::$hash["memcachePort"]		= 11211;

		self::$hash["log"]				= null;
		self::$hash["logLineFormate"]	= "%s : [%s] %s\n";
		self::$hash["logTimeFormate"]	= "c";

		self::$hash["framework"]		= dirname(__FILE__);
		self::$hash["development"]		= false;
		self::$hash["local"]			= "en_US";
		self::$hash["ssl"]				= false;
		
		self::$hash["i18nXmlSchema"]	= "%s/i18n/%s/%s.xml";
		self::$hash["i18nCacheSchema"]	= "%s/cache/i18n/%s/%s";
		self::$hash["classSchema"]		= "%s/classes/%s";
		self::$hash["classDir"]			= "%s/classes/";
		self::$hash["webSchema"]		= "%s/web/%s.html";
		
		self::$hash["gzip"]				= false;
		self::$hash["isGzipped"]		= false;

		self::$hash["cache"]			= "%s/cache/";
		
		/**
		 * overide default values && add more configuration keys
		 */
		foreach ($config as $key => $value) {
			self::$hash[$key] = $value;
		}

		if (isset(self::$hash["secret"])) {
			self::$hash["secret"]	= sha1(sprintf("%s%s", $_SERVER["REMOTE_ADDR"], self::$hash["secret"]));
		}

		self::$hash["classSchema"] .= self::$hash["phpExt"];
		self::$hash["classDir"]		= sprintf(self::$hash["classDir"], self::$hash["path"]);

		self::$hash["cache"]		= sprintf(self::$hash["cache"], self::$hash["path"]);

		/**
		 * if running on web server!
		 */
		if (empty($_SERVER["HTTP_USER_AGENT"]) === false) {
			# $httprotocol = ($_SERVER["HTTPS"] === "on") ? "http" : "https";
			$httprotocol = (self::$hash["ssl"] === false) ? "http" : "https";
			if (isset($config["url"])) {
				self::$hash["url"] = sprintf($config["url"], $_SERVER["HTTP_HOST"]);
				self::$hash["url"] = sprintf("%s://%s", $httprotocol, self::$hash["url"]);
			} else {
				self::$hash["url"] = sprintf ("%s://%s", $httprotocol, $_SERVER["HTTP_HOST"]);
			}
		}
		
		$url = (substr(self::$hash["url"], -1) == "/") ? "%s%s" : "%s/%s";
		if (isset(self::$hash["bootstrap"])) {
			self::$hash["curl"] = sprintf($url, self::$hash["url"], self::$hash["bootstrap"]);
		} else {
			self::$hash["curl"] = sprintf($url, self::$hash["url"], basename($_SERVER["SCRIPT_FILENAME"]));
		}

		self::$hash["address"]	= sprintf("http%s://%s%s%s",
									(isset($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) ? "s" : "",
									$_SERVER["SERVER_NAME"],
									($_SERVER["SERVER_PORT"] != "80") ? sprintf(":%s", $_SERVER["SERVER_PORT"]) : "",
									$_SERVER["REQUEST_URI"]
								);
	}
	
	public function __get($key) {
		if (isset(self::$hash[$key])) {
			return self::$hash[$key];
		} else {
			return null;
		}
	}
	
	public function __set($key, $value) {
		self::$hash[$key] = $value;
	}
}
